home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / VirtualFile / VirtualHFSMacFile.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  6.8 KB  |  279 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        VirtualHFSMacFile.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __TYPES__
  15. #include <Types.h>
  16. #endif
  17.  
  18. #ifndef __MEMORY__
  19. #include <Memory.h>
  20. #endif
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifndef __FILES__
  27. #include <Files.h>
  28. #endif
  29.  
  30. #ifndef __VIRTUALMACFILE__
  31. #include "VirtualMacFile.h"
  32. #endif
  33.  
  34. #ifndef __VIRTUALHFSMACFILE__
  35. #include "VirtualHFSMacFile.h"
  36. #endif
  37.  
  38. #ifndef    __UTILITIES__
  39. #include "Utilities.h"
  40. #endif
  41.  
  42. #ifndef    __DEBUGGINGGEAR__
  43. #include "DebuggingGear.h"
  44. #endif
  45.  
  46. const short kUnopenedRefNum = -1;
  47.  
  48. #pragma segment VirtualHFSFile
  49.  
  50. /***********************************|****************************************/
  51.  
  52. TVirtualHFSMacFile::TVirtualHFSMacFile ( const FSSpec& storage, OSType creator, OSType fileType):
  53.     TVirtualMacFile (),
  54.     fSpec ( storage ),
  55.     fDFRefnum ( kUnopenedRefNum ),
  56.     fRFRefnum ( kUnopenedRefNum ),
  57.     fDisposeFile ( false )
  58. {
  59.     ::FSpCreate(&((TVirtualHFSMacFile*)this)->fSpec,creator,fileType,smRoman);
  60. }
  61.  
  62. //--------------------------------------------------------------------------------
  63.  
  64. TVirtualHFSMacFile::~TVirtualHFSMacFile()
  65. {
  66.     Close();
  67.     if (fDisposeFile)
  68.         FAILOSErr ( ::FSpDelete(&((TVirtualHFSMacFile*)this)->fSpec) );
  69. }
  70.  
  71. //--------------------------------------------------------------------------------
  72. // Returns true if the file will be disposed of by the destructor
  73. Boolean TVirtualHFSMacFile::GetDisposeFile()
  74. {
  75.     return fDisposeFile;
  76. }
  77.  
  78. //--------------------------------------------------------------------------------
  79. // Set to true: file will be disposed of by the destructor
  80. // Set to false: file will remain on disk after this object is deleted
  81. void TVirtualHFSMacFile::SetDisposeFile(Boolean disposeFile)
  82. {
  83.     fDisposeFile = disposeFile;
  84. }
  85.  
  86. //--------------------------------------------------------------------------------
  87.  
  88. OSErr TVirtualHFSMacFile::Open ()
  89. {
  90.     OSErr err = noErr;
  91.  
  92.     if ( fDFRefnum == kUnopenedRefNum )
  93.         err = ::FSpOpenDF(&((TVirtualHFSMacFile*)this)->fSpec,fsRdWrPerm, &fDFRefnum);
  94.  
  95.     if ( err == noErr && fRFRefnum == kUnopenedRefNum )
  96.         err = ::FSpOpenRF(&((TVirtualHFSMacFile*)this)->fSpec,fsRdWrPerm, &fRFRefnum);
  97.  
  98.     return err;
  99. }
  100.  
  101. //--------------------------------------------------------------------------------
  102.  
  103. OSErr TVirtualHFSMacFile::Close ()
  104. {
  105.     OSErr err1 = ::FSClose(fDFRefnum);
  106.     fDFRefnum = kUnopenedRefNum;
  107.  
  108.     OSErr err2 = ::FSClose(fRFRefnum);
  109.     fRFRefnum = kUnopenedRefNum;
  110.  
  111.     if (err1 == noErr)
  112.         err1 = err2;
  113.  
  114.     return err1;
  115. }
  116.  
  117. //--------------------------------------------------------------------------------
  118.  
  119. OSErr TVirtualHFSMacFile::ReadData (void* buffer,long& count,TVirtualMacFile::ForkType whichFork) 
  120. {
  121.     return ::FSRead(GetRefNum(whichFork),&count, buffer);
  122. }
  123.  
  124. //--------------------------------------------------------------------------------
  125.  
  126. OSErr TVirtualHFSMacFile::WriteData (const void* buffer, long& count, TVirtualMacFile::ForkType whichFork)
  127. {
  128.     return ::FSWrite(GetRefNum(whichFork),&count, buffer);
  129. }
  130.  
  131. //--------------------------------------------------------------------------------
  132.  
  133. OSErr TVirtualHFSMacFile::SetEnd (long logEof, TVirtualMacFile::ForkType whichFork)
  134. {
  135.     return ::SetEOF (GetRefNum(whichFork),logEof);
  136. }
  137.  
  138. //--------------------------------------------------------------------------------
  139.  
  140. OSErr TVirtualHFSMacFile::GetEnd (long& logEof, TVirtualMacFile::ForkType whichFork) const
  141. {
  142.     return ::GetEOF (GetRefNum(whichFork),&logEof);
  143. }
  144.  
  145. //--------------------------------------------------------------------------------
  146.  
  147. OSErr TVirtualHFSMacFile::SetPosition (short posMode, long posOff, TVirtualMacFile::ForkType whichFork)
  148. {
  149.     return ::SetFPos (GetRefNum(whichFork),posMode,posOff);
  150. }
  151.  
  152. //--------------------------------------------------------------------------------
  153.  
  154. OSErr TVirtualHFSMacFile::GetPosition (long& filePos,TVirtualMacFile::ForkType whichFork) const
  155. {
  156.     return ::GetFPos (GetRefNum(whichFork),&filePos);
  157. }
  158.  
  159. //--------------------------------------------------------------------------------
  160.  
  161. OSErr TVirtualHFSMacFile::SetFinderInfo (const FInfo& finderInfo)
  162. {
  163.     return ::FSpSetFInfo(&((TVirtualHFSMacFile*)this)->fSpec,&finderInfo);
  164. }
  165.  
  166. //--------------------------------------------------------------------------------
  167.  
  168. OSErr TVirtualHFSMacFile::GetFinderInfo (FInfo& finderInfo)  const
  169. {
  170.     return ::FSpGetFInfo(&((TVirtualHFSMacFile*)this)->fSpec,&finderInfo);
  171. }
  172.  
  173. //--------------------------------------------------------------------------------
  174.  
  175. void TVirtualHFSMacFile::SetUserRef(long ref)
  176. {
  177.     fUserRef = ref;
  178. }
  179.  
  180. //--------------------------------------------------------------------------------
  181.  
  182. long TVirtualHFSMacFile::GetUserRef()  const
  183. {
  184.     return fUserRef;
  185. }
  186.  
  187. //--------------------------------------------------------------------------------
  188.  
  189. OSErr TVirtualHFSMacFile::GetDate (unsigned long& dateTime,TVirtualMacFile::WhichDateType whichDate) const
  190. {
  191.     HParamBlockRec paramBlock;
  192.     FSSpec spec = fSpec;
  193.  
  194.     OSErr err = GetFileInfo(spec, paramBlock);
  195.  
  196.     if (whichDate == kCreationDate)
  197.         dateTime = paramBlock.fileParam.ioFlCrDat;
  198.     else
  199.         dateTime = paramBlock.fileParam.ioFlMdDat;
  200.  
  201.     return err;
  202. }
  203.  
  204. //--------------------------------------------------------------------------------
  205.  
  206. OSErr TVirtualHFSMacFile::SetDate (unsigned long dateTime,TVirtualMacFile::WhichDateType whichDate)
  207. {
  208.     HParamBlockRec paramBlock;
  209.     FSSpec spec = fSpec;
  210.  
  211.     OSErr err = GetFileInfo(spec, paramBlock);
  212.  
  213.     if (whichDate == kCreationDate)
  214.         paramBlock.fileParam.ioFlCrDat = dateTime;
  215.     else
  216.         paramBlock.fileParam.ioFlMdDat = dateTime;
  217.  
  218.     paramBlock.fileParam.ioDirID = spec.parID;
  219.     if (err == noErr)
  220.         err = PBHSetFInfo(¶mBlock, false);
  221.     return err;
  222. }
  223.  
  224. //--------------------------------------------------------------------------------
  225.  
  226. OSErr TVirtualHFSMacFile::SetSpec ( const FSSpec& spec )
  227. {
  228.     OSErr error = noErr;
  229.  
  230.     if ( fSpec.vRefNum == spec.vRefNum )
  231.     {
  232.         if ( spec.parID != fSpec.parID )
  233.         {
  234.             error = ::MoveHFSFile ( fSpec.vRefNum, fSpec.parID, fSpec.name, spec.parID );
  235.             
  236.             if ( !error )
  237.                 fSpec.parID = spec.parID;
  238.         }
  239.  
  240.         error = ::FSpRename ( &fSpec, spec.name );
  241.         
  242.         if ( !error )
  243.             ::PLstrcpy ( fSpec.name, spec.name );
  244.     }
  245.     else
  246.     {
  247.         error = nsvErr;
  248.     }
  249.  
  250.     return error;
  251. }
  252.  
  253. //--------------------------------------------------------------------------------
  254.  
  255. OSErr TVirtualHFSMacFile::GetSpec ( FSSpec& spec ) const
  256. {
  257.     spec = fSpec;
  258.     return noErr;
  259. }
  260.  
  261. /***********************************|****************************************/
  262.  
  263. OSErr     
  264. TVirtualHFSMacFile::WriteToDisk ( const FSSpec& spec )
  265. {
  266.     return TVirtualMacFile::WriteToDisk ( spec );
  267. }
  268.  
  269. /***********************************|****************************************/
  270.  
  271. OSErr     
  272. TVirtualHFSMacFile::WriteToDisk ()
  273. {
  274.     return noErr;    // we are already on the disk!
  275. }
  276.  
  277. /***********************************|****************************************/
  278.  
  279.